﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Office.Interop.Excel;
using MVVM.Core;

namespace ObjectModel.Excel.Old
{
    /// <summary>
    /// Holds serializable information about a range object's parent workbook and worksheet.
    /// </summary>
    public abstract class OfflineRange : NotificationBase
    {
        private bool mCloseWorkbook;
        private string mWorkbookFullName;
        private string mWorkbookName;
        private string mWorksheetName;

        [XmlIgnore]
        public Application Application { get;  set; }


        // Allows for marking of the source workbook as having been opened programmatically
        // and therefore to be closed by the controlling operation.
        public bool CloseWorkbook { get { return mCloseWorkbook; } }

        // Needed to get a reference to a workbook object that is currently not open.
        public string WorkbookFullName // To be serialized. Must be public.
        {
            get { return mWorkbookFullName; }
            set
            {
                mWorkbookFullName = value;
                OnPropertyChanged("WorkbookFullName");
            }
        }

        // Needed to get a reference to a workbook object that is already open in the current app.
        public string WorkbookName // To be serialized. Must be public.
        {
            get { return mWorkbookName; }
            set
            {
                mWorkbookName = value;
                OnPropertyChanged("WorkbookName");
            }
        }

        public string WorksheetName  // To be serialized. Must be public.
        {
            get { return mWorksheetName; }
            set
            {
                mWorksheetName = value;
                OnPropertyChanged("WorksheetName");
            }
        }

        [XmlIgnore]
        public virtual Range Range
        {
            get { return null; }
            set
            {
                Worksheet = value.Worksheet;
                Workbook = value.Worksheet.Parent;
                OnPropertyChanged("Range");
            }
        }

        [XmlIgnore]
        public virtual Workbook Workbook
        {
            get { return Application.GetWorkbookFromNames(WorkbookName, WorkbookFullName, out mCloseWorkbook); }
            private set
            {
                mWorkbookFullName = value.FullName;
                mWorkbookName = value.Name;
            }
        }

        [XmlIgnore]
        public virtual Worksheet Worksheet
        {
            get { return Workbook.GetWorksheetFromName(WorksheetName); }
            private set { mWorksheetName = value.Name; }
        }
    }
}
